﻿using DAL;
using Entity.Models.Entities;
using IBLL;
using IDAL;
using RepositorySys.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Utils;
using Utils.enums;

namespace BLL
{
    public class LoginBLL : ILoginBLL
    {
        IUserDAL userDAL;
        IFileInfoDAL _fileInfoDAL;
        public LoginBLL(IUserDAL userDAL, IFileInfoDAL fileInfoDAL)
        {
            this.userDAL = userDAL;
            this._fileInfoDAL = fileInfoDAL;
        }

        /// <summary>
        /// 判断账号密码是否正确
        /// </summary>
        /// <param name="userInfoDTO"></param>
        /// <returns></returns>
        public bool Login(UserInfoDTO userInfoDTO, out string mgs, out string userName, out string userId, out string userImg)
        {
            UserInfo byuserInfo = userDAL.getUserShowDAL().FirstOrDefault(d => d.IsDelete != true && d.Account == userInfoDTO._account);
            if (byuserInfo != null)
            {
                string newPwd = MD5.md5Encrypt(userInfoDTO._passWord);
                if (String.Equals(byuserInfo.PassWord, newPwd))
                {
                    userName = byuserInfo.UserName;
                    userId = byuserInfo.Id;
                    var fileInfo = _fileInfoDAL.getEntitys().Where(f => f.RelationId.EndsWith(byuserInfo.Id)).OrderByDescending(f => f.CreateTime).ToList();
                    if (fileInfo.Count < 1)
                    {
                        userImg = null;
                        mgs = Enums.getDescribe(LoginEnums.REGITER_STATES.SUCCEED);
                        return true;
                    }
                    userImg = FileHelper.PhotoUrl(fileInfo.First().NewFileName);
                    mgs = Enums.getDescribe(LoginEnums.REGITER_STATES.SUCCEED);
                    return true;
                }
                mgs = Enums.getDescribe(LoginEnums.LOGIN_STATE.ACCOUNT_PWD);
            }
            else
            {
                mgs = Enums.getDescribe(LoginEnums.LOGIN_STATE.ACCOUNT_NOT);
            }
            userName = null;
            userId = null;
            userImg = null;
            return false;
        }
    }
}
